use axum::{http::Request, middleware::Next, response::Response};
use crate::utils::error::AppError;

// JWT 认证中间件
pub async fn auth<B>(
    request: Request<B>,
    next: Next<B>,
) -> Result<Response, AppError> {
    let token = request.headers()
        .get("Authorization")
        .and_then(|h| h.to_str().ok())
        .ok_or(AppError::Unauthorized)?;

    let claims = verify_jwt(token)?;
    request.extensions_mut().insert(claims.user_id);
    Ok(next.run(request).await)
}